package com.xing.ucenter.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xing.servicebase.exceptionhandler.GuliException;
import com.xing.ucenter.controller.vo.LoginVo;
import com.xing.ucenter.controller.vo.RegisterVo;
import com.xing.ucenter.entity.LoginInfoVo;
import com.xing.ucenter.entity.Member;
import com.xing.ucenter.mapper.MemberMapper;
import com.xing.ucenter.service.MemberService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xing.ucenter.utils.JwtUtils;
import com.xing.ucenter.utils.MD5;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Date;


/**
 * <p>
 * 会员表 服务实现类
 * </p>
 *
 * @author xingfuao
 * @since 2022-03-30
 */
@Service
public class MemberServiceImpl extends ServiceImpl<MemberMapper, Member> implements MemberService {


    @Autowired
    private RedisTemplate<String, String> redisTemplate;

    /**
     * 会员登录
     *
     * @param loginVo
     * @return
     */
    @Override
    public String login(LoginVo loginVo) {
        String mobile = loginVo.getMobile();
        String password = loginVo.getPassword();

        //检验参数
        if (StringUtils.isEmpty(mobile) || StringUtils.isEmpty(password)) {
            throw new GuliException(20001, "error");
        }

        //获取会员
        QueryWrapper<Member> mobile1 = new QueryWrapper<Member>()
                .eq("mobile", mobile);
        Member member = baseMapper.selectOne(mobile1);

        if (null == member) {
            throw new GuliException(20001, "error");
        }
        String encrypt11 = MD5.encrypt(password);
        System.out.println("加密后的密码：" + encrypt11);

        //检验密码
        if (!MD5.encrypt(password).equals(member.getPassword())) {
            throw new GuliException(20001, "error");
        }
        //检验是否被禁用
        if (member.getIsDisabled()) {
            throw new GuliException(20001, "error");
        }
        //使用JWT生成token字符串
        String token = JwtUtils.getJwtToken(member.getId(), member.getNickname());
        return token;
    }


    /**
     * 会员注册
     *
     * @param registerVo
     */
    @Override
    public void register(RegisterVo registerVo) {

        //获取注册信息，进项校验
        String nickname = registerVo.getNickname();
        String mobile = registerVo.getMobile();
        String password = registerVo.getPassword();
        String code = registerVo.getCode();

        //校验参数
        if (StringUtils.isEmpty(mobile) ||
                StringUtils.isEmpty(nickname) ||
                StringUtils.isEmpty(password) ||
                StringUtils.isEmpty(code)) {
            throw new GuliException(20001, "error");
        }
        System.out.println("手机号：" + mobile);
        //校验验证码
        //从redis获取发送的验证码
        String mobleCode = redisTemplate.opsForValue().get(mobile);

        System.out.println("验证码=====>:" + mobile);
        if (!code.equals(mobleCode)) {
            throw new GuliException(20001, "error");
        }
        //查询数据库中是否存在相同的手机号
        QueryWrapper<Member> memberQueryWrapper = new QueryWrapper<Member>().eq("mobile", mobile);
        Integer count = baseMapper.selectCount(memberQueryWrapper);
        if (count.intValue() > 0) {
            throw new GuliException(20001, "error");
        }
        //听见注册信息到数据库中
        Member member = new Member();
        member.setNickname(nickname);
        member.setMobile(registerVo.getMobile());
        member.setPassword(MD5.encrypt(password));
        member.setIsDisabled(false);
        member.setGmtCreate(new Date());
        member.setGmtModified(new Date());
        member.setAvatar("http://thirdwx.qlogo.cn/mmopen/vi_32/DYAIOgq83eoj0hHXhgJNOTSOFsS4uZs8x1ConecaVOB8eIl115xmJZcT4oCicvia7wMEufibKtTLqiaJeanU2Lpg3w/132");
        this.save(member);
    }

    @Override
    public LoginInfoVo getloginInfo(String memberId) {
        Member member = baseMapper.selectById(memberId);
        LoginInfoVo loginInfoVo = new LoginInfoVo();
        //浅拷贝
        BeanUtils.copyProperties(member, loginInfoVo);
        return loginInfoVo;
    }

    /**
     * 判断该微信信息是否注册过
     *
     * @param openid
     * @return
     */
    @Override
    public Member getMemberByOpenid(String openid) {
        QueryWrapper<Member> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("openid", openid);
        Member member = baseMapper.selectOne(queryWrapper);
        return member;
    }

    /**
     * 根据时间，获取那天注册的人数
     * @param day
     * @return
     */
    @Override
    public Integer getCountRegister(String day) {
        return baseMapper.getCountRegister(day);
    }
}
